<template>
  <div class="com-update-gender">
    <van-picker
      title="性别"
      show-toolbar
      :columns="colunms"
      :default-index="defaultIndex"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      colunms: ['男', '女'],
      defaultIndex: this.value
    }
  },
  methods: {
    onChange(picker, value, index) {
      this.defaultIndex = index
    },
    async onConfirm() {
      this.$toast.loading({
        message: '修改中'
      })

      await updateUserProfile({
        gender: this.defaultIndex
      })

      this.$emit('input', this.defaultIndex)
      this.$toast.success('修改成功')
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
.com-update-name {
  height: 100%;
  background-color: #f5f7f9;
  .input-wrap {
    padding: 10px;
  }
}
</style>
